package com.hengda.smart.speak.d.util;

import android.content.Context;
import android.util.Log;

import com.hengda.smart.speak.d.bean.DDTask;
import com.hengda.smart.speak.d.bean.TeamPosition;
import com.hengda.smart.speak.d.bean.UserInfo;
import com.hengda.smart.speak.d.webservice.HTTPFieldConstant;
import com.hengda.smart.speak.d.webservice.WebData;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.utils.KJLoger;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;
import java.util.List;


/**
 * 
* @Description TODO
* @author wzq
* @date 2015-6-13 ����1:06:02
* @update (date)
* @version V1.0
 */
public class ParserUtil implements ParserInterface {




	private static final ParserUtil parserUtil=new ParserUtil();

	private ParserUtil(){
	}

	public static  ParserUtil  getInstance(){
		return parserUtil;
	}


	public String TAG =this.getClass().getName();

	public static <T> T xmlToBean(Class<T> type, String xml) {
		T data = null;
		try {
			XStream xStream = new XStream(new DomDriver("UTF-8"));
			xStream.processAnnotations(type);
			data = (T) xStream.fromXML(xml);
		} catch (Exception e) {
			try {
				data = type.newInstance();
			} catch (Exception ee) {
			} finally {
			}
		}
		return data;
	}

	/**
	 * 检测更新
	 * @param json
	 */
	public static String checkVersion(Context cxt, String json) {
		String url = "";
		try {
			JSONObject obj = new JSONObject(json);
			int serverVersion = obj.optInt("version", 0);
			int currentVersion = SystemTool.getAppVersionCode(cxt);
			KJLoger.debug("当前版本：" + currentVersion + "最新版本：" + serverVersion);
			if (serverVersion > currentVersion) {
				url = obj.optString("url");
			}
		} catch (JSONException e) {
			Log.e("kymjs", "getBlogList()解析异常");
		}
		return url;
	}


	@Override
	public UserInfo parserUserInfo(String json) {
		UserInfo info=new UserInfo();
		try {
			JSONArray array=new JSONArray(json);
			JSONObject joj=(JSONObject)array.get(0);
			info.setUserWorkNo(joj.optString(HTTPFieldConstant.JSON_USERINFO_ID, ""));
			info.setUserJObNum(joj.optString(HTTPFieldConstant.JSON_USERINFO_JOBNUM, ""));
			info.setUser(joj.optString(HTTPFieldConstant.JSON_USERINFO_User, ""));
			info.setName(joj.optString(HTTPFieldConstant.JSON_USERINFO_Name, ""));
			info.setHandUrl(joj.optString(HTTPFieldConstant.JSON_USERINFO_HEADURL, ""));
//			info.setSex(joj.optString(HTTPFieldConstant.JSON_USERINFO_Name, ""));
			info.setTel(joj.optString(HTTPFieldConstant.JSON_USERINFO_Tel, ""));
			info.setCard(joj.optString(HTTPFieldConstant.JSON_USERINFO_Card, ""));
			info.setEntryTime(joj.optString(HTTPFieldConstant.JSON_USERINFO_EntryTime, ""));
			info.setStartGrade(joj.optString(HTTPFieldConstant.JSON_USERINFO_Grade, ""));
			info.setUUID(joj.optString(HTTPFieldConstant.JSON_USERINFO_UUID, ""));
			return info;
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<DDTask> parserUsertask(String json) {
		List<DDTask> taskList=new ArrayList<DDTask>();

		try {
			JSONArray array=new JSONArray(json);
			for (int i = 0; i < array.length(); i++) {
				DDTask  task = new DDTask();
				task.setId(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_ID, ""));
				task.setName(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_NAME, ""));
				task.setTel(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_TEL, ""));
				task.setUnit(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_UNIT, ""));
				task.setNumber(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_NUMBER, ""));
				task.setDescription(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_DESCRIPTION, ""));
				task.setDate(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_DATE, ""));
				task.setTime(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_TIME, ""));
				task.setStatus(array.getJSONObject(i).optInt(HTTPFieldConstant.TASK_STATUS, 1));

				task.setStar1No(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_REMARK_STAR1, "0"));
				task.setStar2No(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_REMARK_STAR2, "0"));
				task.setStar3No(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_REMARK_STAR3, "0"));
				task.setStar4No(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_REMARK_STAR4, "0"));
				task.setStar5No(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_REMARK_STAR5, "0"));
				taskList.add(task);
			}
			return taskList;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}


	@Override
	public String parserTokenInfo(String json) {
		WebData jsData=new WebData();
		try {
			JSONObject joj=new JSONObject(json);
			jsData.setWebCode(joj.getString(HTTPFieldConstant.JSON_BASEDATA_CODE));
			try {
				jsData.setWebData(new String(joj.getString(HTTPFieldConstant.JSON_BASEDATA_TOKEN)));
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(jsData.getWebCode().equals("001")){
				return jsData.getWebData();
			}else{
				return "";
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}

	}


	@Override
	public WebData parserWebJsonData(String json) {
		WebData jsData=new WebData();
		try {
			JSONObject joj=new JSONObject(json);
			jsData.setWebCode   (joj.optString(HTTPFieldConstant.JSON_BASEDATA_CODE));
			jsData.setWebData   (joj.optString(HTTPFieldConstant.JSON_BASEDATA_DATA));
			jsData.setLoginToken(joj.optString(HTTPFieldConstant.JSON_BASEDATA_LOGINTOKEN));
			jsData.setTotalNum(joj.optInt(HTTPFieldConstant.JSON_BASEDATA_TOTAL));
			return jsData;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<TeamPosition> parserTeamPosition(String json) {
		List<TeamPosition> positionList=new ArrayList<TeamPosition>();
		try {
			JSONArray array=new JSONArray(json);
			for (int i = 0; i < array.length(); i++) {
				TeamPosition position=new TeamPosition();
				position.setAreaNo(array.getJSONObject(i).optInt(HTTPFieldConstant.TEAM_AREANO));
				position.setDocId (array.getJSONObject(i).optInt(HTTPFieldConstant.TEAM_DOCID));
				position.setTaskId(array.getJSONObject(i).optInt(HTTPFieldConstant.TEAM_TASKID));
				position.setPointX(array.getJSONObject(i).optInt(HTTPFieldConstant.TEAM_POINTX,0));
				position.setPointY(array.getJSONObject(i).optInt(HTTPFieldConstant.TEAM_POINTY,0));
				position.setAreaName(array.getJSONObject(i).optString(HTTPFieldConstant.TEAM_AREA_NAME,""));
				position.setLeaderName(array.getJSONObject(i).optString(HTTPFieldConstant.TEAM_LEADER,""));
				position.setStartTime(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_START_TIME,""));
				position.setTaskPeopleNum(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_NUMBER,""));
				position.setTaskUnit(array.getJSONObject(i).optString(HTTPFieldConstant.TASK_UNIT,""));
				position.setJobNo(array.getJSONObject(i).optString("JobNum",""));
				positionList.add(position);
			}
			return positionList;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}

	}


	@Override
	public DDTask parserTask(String json) {

		DDTask  task = new DDTask();
		try {
			JSONArray array=new JSONArray(json);
			JSONObject obj = (JSONObject)array.get(0);
			task.setId(obj.optString(HTTPFieldConstant.TASK_ID, ""));
			task.setName(obj.optString(HTTPFieldConstant.TASK_NAME, ""));
			task.setTel(obj.optString(HTTPFieldConstant.TASK_TEL, ""));
			task.setUnit(obj.optString(HTTPFieldConstant.TASK_UNIT, ""));
			task.setNumber(obj.optString(HTTPFieldConstant.TASK_NUMBER, ""));
			task.setDescription(obj.optString(HTTPFieldConstant.TASK_DESCRIPTION, ""));
			task.setDate(obj.optString(HTTPFieldConstant.TASK_DATE, ""));
			task.setTime(obj.optString(HTTPFieldConstant.TASK_TIME, ""));
			task.setStatus(obj.optInt(HTTPFieldConstant.TASK_STATUS, 1));
			return task;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public WebData parserWebJsonDataPositon(String json) {

		WebData jsData=new WebData();
		try {
			JSONObject joj=new JSONObject(json);
			jsData.setWebCode   (joj.optString(HTTPFieldConstant.JSON_BASEDATA_CODE));
			jsData.setWebData   (joj.optString(HTTPFieldConstant.JSON_BASEDATA_DATA));
			jsData.setMapId(joj.optString("mapId",""));
			jsData.setLoginToken(joj.optString(HTTPFieldConstant.JSON_BASEDATA_LOGINTOKEN));
			jsData.setTotalNum(joj.optInt(HTTPFieldConstant.JSON_BASEDATA_TOTAL));
			return jsData;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}



}
